﻿using System.Web;
using System.Web.Mvc;
using Mvc.XForms.Utilities;

namespace Mvc.XForms.UI {
	public static class PagingExtensions {
		public static void Paging(this HtmlHelper htmlHelper, IPaged paged) {
			Paging(htmlHelper.ViewContext.HttpContext, paged, null);
		}

		public static void Paging(this HtmlHelper htmlHelper, IPaged paged, IPagerFormatter pagerFormatter) {
			Paging(htmlHelper.ViewContext.HttpContext, paged, pagerFormatter);
		}

		public static void Paging(HttpContextBase context, IPaged paged, IPagerFormatter pagerFormatter) {
			var varsToReplace = new[] { paged.PageIndexKey };
			var pagedUrlTemplate = context.Request.UrlFormat(varsToReplace, true);

			Paging(context.Response, paged, pagerFormatter, pagedUrlTemplate);
		}

		public static void Paging(HttpResponseBase response, IPaged paged, IPagerFormatter pagerFormatter, string pagedUrlTemplate) {
			var formatter = pagerFormatter ?? GridFormatterFactory.Current.PagerFormatter;
			var paging = formatter.Pager(paged, pagedUrlTemplate);
			response.Write(paging);
		}
	}
}